package com.example.practice.controller;

import com.example.practice.entity.User;
import com.example.practice.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

@RestController
@RequestMapping("/api/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 用户登录认证
     * @param loginRequest 包含用户名和密码的请求体
     * @return 认证结果
     */
    @PostMapping("/login")
    public Map<String, Object> login(@RequestBody LoginRequest loginRequest) {
        Map<String, Object> response = new HashMap<>();
        
        // 验证用户名和密码
        if (userService.login(loginRequest.getUsername(), loginRequest.getPassword())) {
            response.put("success", true);
            response.put("message", "登录成功");
            response.put("username", loginRequest.getUsername());
        } else {
            response.put("success", false);
            response.put("message", "用户名或密码错误");
        }
        
        return response;
    }

    /**
     * 用户注册
     * @param registerRequest 包含用户名、密码和确认密码的请求体
     * @return 注册结果
     */
    @PostMapping("/register")
    public Map<String, Object> register(@RequestBody RegisterRequest registerRequest) {
        Map<String, Object> response = new HashMap<>();
        
        // 验证密码和确认密码是否一致
        if (!registerRequest.getPassword().equals(registerRequest.getConfirmPassword())) {
            response.put("success", false);
            response.put("message", "密码和确认密码不一致");
            return response;
        }
        
        // 验证验证码（简单验证，实际项目中应该更复杂）
        if (!isValidCaptcha(registerRequest.getCaptcha())) {
            response.put("success", false);
            response.put("message", "验证码错误");
            return response;
        }
        
        // 注册用户
        if (userService.register(registerRequest.getUsername(), registerRequest.getPassword())) {
            response.put("success", true);
            response.put("message", "注册成功");
        } else {
            response.put("success", false);
            response.put("message", "用户名已存在");
        }
        
        return response;
    }

    /**
     * 验证用户是否已登录
     * @param username 用户名
     * @return 验证结果
     */
    @GetMapping("/validate")
    public Map<String, Object> validateUser(@RequestParam String username) {
        Map<String, Object> response = new HashMap<>();
        
        Optional<User> userOptional = userService.getUserByUsername(username);
        if (userOptional.isPresent()) {
            response.put("valid", true);
            response.put("username", username);
        } else {
            response.put("valid", false);
        }
        
        return response;
    }

    /**
     * 生成验证码
     * @return 验证码
     */
    @GetMapping("/captcha")
    public Map<String, Object> generateCaptcha() {
        Map<String, Object> response = new HashMap<>();
        // 生成简单的4位数字验证码
        String captcha = String.valueOf((int)(Math.random() * 9000) + 1000);
        response.put("captcha", captcha);
        return response;
    }

    /**
     * 简单的验证码验证（实际项目中应该更复杂）
     * @param captcha 用户输入的验证码
     * @return 是否有效
     */
    private boolean isValidCaptcha(String captcha) {
        // 这里应该实现更复杂的验证码验证逻辑
        // 暂时简单返回true，实际项目中应该验证验证码的正确性和时效性
        return captcha != null && captcha.length() == 4;
    }

    // 登录请求的内部类
    static class LoginRequest {
        private String username;
        private String password;

        public String getUsername() {
            return username;
        }

        public void setUsername(String username) {
            this.username = username;
        }

        public String getPassword() {
            return password;
        }

        public void setPassword(String password) {
            this.password = password;
        }
    }

    // 注册请求的内部类
    static class RegisterRequest {
        private String username;
        private String password;
        private String confirmPassword;
        private String captcha;

        public String getUsername() {
            return username;
        }

        public void setUsername(String username) {
            this.username = username;
        }

        public String getPassword() {
            return password;
        }

        public void setPassword(String password) {
            this.password = password;
        }

        public String getConfirmPassword() {
            return confirmPassword;
        }

        public void setConfirmPassword(String confirmPassword) {
            this.confirmPassword = confirmPassword;
        }

        public String getCaptcha() {
            return captcha;
        }

        public void setCaptcha(String captcha) {
            this.captcha = captcha;
        }
    }
}